package com.zmcn.msg.fw.tool.sl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.zmcn.msg.fw.util.FileOP;

public class Power_my {
	private static String config = "/opData/power_my.xml";
	private static String dbName = "MSGDB";
	private static String outPath = "E:/mystore/MsgServer/script/sys2.sql";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path = Power_my.class.getResource(config).getFile();
		String c = FileOP.readFile(path, "utf-8");
		String[] cc = c.split("\r\n");
		String moduleName = "";
		String opName = "";
		String mdId = "";
		String link;
		String linkId;
		List<String> mds = new ArrayList<String>();
		List<String> links = new ArrayList<String>();
		List<String> tmplinks = null;

		List<String> allModuleIds = new ArrayList<String>();

		int p = 1;
		for (String s : cc) {
			if ("".equals(s))
				continue;
			if (s.startsWith("----------->")) {
				moduleName = s.substring("----------->".length());
			}
			if (s.startsWith("=")) {
				opName = s.substring("=".length());
				mdId = UUID.randomUUID().toString().replaceAll("-", "");
				tmplinks = new ArrayList<String>();
				allModuleIds.add(mdId);
				mds.add("insert into T_SYS_OPERATE (OPERATE_ID, MODULE_NAME, OPERATE_NAME, ORDER_KEY) values ('" + mdId + "', '" + moduleName + "', '" + opName + "', " + p + ");");
				p++;
			}
			if (s.startsWith("---->")) {
				link = s.substring("---->".length());
				if (!tmplinks.contains(link)) {
					tmplinks.add(link);
					linkId = UUID.randomUUID().toString().replaceAll("-", "");
					links.add("insert into T_LINK (ID, OPERATE_ID, NAME, LINK) values ('" + linkId + "', '" + mdId + "', '" + moduleName + "-" + opName + "', '" + link + "');");
				}
			}

		}

		StringBuffer sb = new StringBuffer("use " + dbName + ";\r\n");
		//
		sb.append("\r\n\r\n#=========================================模块和操作列表\r\n");
		for (String s : mds) {
			sb.append(s + "\r\n");
		}
		sb.append("\r\n\r\n#=========================================操作对应的链接\r\n");
		for (String s : links) {
			sb.append(s + "\r\n");
		}

		sb.append("\r\n\r\n#=========================================给admin角色分配操作权限\r\n");
		for (String s : allModuleIds) {
			String id = UUID.randomUUID().toString().replaceAll("-", "");
			sb.append("insert into T_ROLE_OPERATE (ID, ROLE_ID, OPERATE_ID) values ('" + id + "', 'admin', '" + s + "');\r\n");
		}

		System.out.println(sb);
		FileOP.writeFile(outPath, sb.toString(), "gb2312");
	}
}
